﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GameDataTempshitEditor.Widgets;
using Red.GameData;

namespace GameDataTempshitEditor.DataEditors
{
    public partial class DataEditor_PropertyGrid : UserControl, IDataObjectEditor
    {
        // edition context for the object
        private EditorContext _EditorContext;

        // root editor view for the object
        private EditorObjectView _RootEditorView;

        //---------------------

        public delegate void ObjectChangedHandler(object sender, ObjectChangeEventArgs e);
        public event ObjectChangedHandler OnObjectChanged;

        //---------------------

        Red.GameData.Raw.DataObject IDataObjectEditor.GetObject()
        {
            return _RootEditorView.RawData; // the root object being edited
        }

        Red.Core.UndoRedo.ActionHistory IDataObjectEditor.GetActionHistory()
        {
            return _EditorContext.ActionHistory; // the undo/redo action history connected to current editor
        }

        void IDataObjectEditor.RefreshValues()
        {
            dataPropertyPanel1.RefreshValues();
        }

        public DataEditor_PropertyGrid( EditorContext editorContext, EditorObjectView rootView )
        {
            InitializeComponent();

            // we need the edition context to edit ANYTHING
            _EditorContext = editorContext;
            _RootEditorView = rootView;

            // crap
            dataPropertyPanel1.ImageList = imageList1;

            // create root node in property grid that will edit the root object
            // this is the integration code between GENERAL PropertyGrid and an implementation of the model for the EditorObjectView
            // all actions are routed to the _EditorContext
            var root = new GameData_PropertyGroup_Root(null,_EditorContext, _RootEditorView, true);
            dataPropertyPanel1.Root = root;

            // bind with undo/redo stuff
            _EditorContext.ActionHistory.OnActionRedone += PostUndoRedoAction;
            _EditorContext.ActionHistory.OnActionUndone += PostUndoRedoAction;
            _EditorContext.ActionHistory.OnActionExecuted += PostNormalAction;
        }

        private void PostUndoRedoAction(object sender, EventArgs e)
        {
            dataPropertyPanel1.RefreshValues();

            if (OnObjectChanged != null)
                OnObjectChanged(this, new ObjectChangeEventArgs(_RootEditorView.RawData, _RootEditorView.ReflectionClass));
        }

        private void PostNormalAction(object sender, EventArgs e)
        {
            dataPropertyPanel1.RefreshValues();

            if (OnObjectChanged != null)
                OnObjectChanged(this, new ObjectChangeEventArgs(_RootEditorView.RawData, _RootEditorView.ReflectionClass));
        }

        //---------------------
        
    }
}
